/**
 * 
 */
package com.td.framework.webmanager.xtbm.dao.impl;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.td.framework.webmanager.domain.AppsysXtbm;
import com.td.framework.webmanager.domain.AppsysXtbmBean;
import com.td.framework.webmanager.xtbm.dao.XtbmDao;

/**
 * 
 * Title:系统编码数据层实现类 Description: 实现数据层系统编码处理方法 Copyright: Copyright (c) 2008
 * Company 北京水软科技有限公司
 * 
 * @author FLT006
 * @version 1.0
 */
public class XtbmDaoImpl extends HibernateDaoSupport implements XtbmDao {
	private static final Log log = LogFactory.getLog(XtbmDaoImpl.class);
	private String sql = "";// sql语句;
	private boolean delStatus = false;

	/**
	 * toDo:get
	 * 
	 * @return delStatus
	 */
	public boolean isDelStatus() {
		return delStatus;
	}

	/**
	 * toDo:set
	 * 
	 * @param delStatus
	 */
	public void setDelStatus(boolean delStatus) {
		this.delStatus = delStatus;
	}

	/**
	 * toDo:通过用户编号查询系统编码列表;
	 * 
	 * @param yhbh
	 *            用户编号
	 * @param yhlb
	 *            用户类别
	 * @return list
	 */
	@SuppressWarnings("unchecked")
	public List<AppsysXtbmBean> findXtbmBeanByYhbh(String yhbh, int yhlb) {
		List<AppsysXtbmBean> list = null;
		;
		sql = "select t.id,t.xtmc,t.xtbm,t.beizhu ,nvl(a.flag ,0) as flag "
				+ "from appsys_xtbm t left join ( "
				+ "select (1) as flag ,a.xtbm from appsys_xtbmqx a "
				+ "where a.scbz=0 and a.yhbh='" + yhbh + "' and a.yhlb=" + yhlb
				+ ") a " + "on a.xtbm=t.xtbm where t.scbz=0 order by t.id ";
		Session session = getSession();
		List<Object[]> tempList = session.createSQLQuery(sql).list();
		if (tempList != null && !tempList.isEmpty()) {
			AppsysXtbmBean xtbmBean = null;
			list = new ArrayList();
			for (Iterator iterator = tempList.iterator(); iterator.hasNext();) {
				Object[] objects = (Object[]) iterator.next();
				xtbmBean = new AppsysXtbmBean();
				xtbmBean.setId(Long.parseLong(objects[0] + ""));
				xtbmBean.setXtmc(objects[1] + "");
				xtbmBean.setXtbm(objects[2] + "");
				xtbmBean.setBeizhu(objects[3] + "");
				xtbmBean.setIsChecked(Long.parseLong(objects[4] + ""));
				list.add(xtbmBean);
			}
		}
		return list;
	}

	/**
	 * toDo:保存系统编码;
	 * 
	 * @param appsysXtbm
	 * @return flag
	 */
	public boolean addXtbm(AppsysXtbm appsysXtbm) {
		boolean flag = false;
		this.getHibernateTemplate().save(appsysXtbm);
		return flag;
	}

	/**
	 * toDo:更新系统编码;
	 * 
	 * @param appsysXtbm
	 * @return flag
	 */
	public boolean updateXtbm(AppsysXtbm appsysXtbm) {
		boolean flag = false;
		this.getHibernateTemplate().update(appsysXtbm);
		return flag;
	}

	/**
	 * toDo:删除系统编码;
	 * 
	 * @param xtbh
	 * @return flag
	 */
	public boolean deleteXtbh(String xtbh) {
		boolean flag = false;

		return flag;
	}

	/**
	 * toDo:通过Id查询系统编码对象;
	 * 
	 * @param id
	 * @return AppsysXtbm
	 */
	public AppsysXtbm getAppsysXtbm(Long id) {
		return (AppsysXtbm) this.getHibernateTemplate().get(AppsysXtbm.class,
				id);
	}

	/**
	 * toDo:保存用户/用户组系统编码权限
	 * 
	 * @param xtbms
	 * @param yhbh
	 * @param yhlx
	 * @param bmbms
	 * @return flag;
	 */
	public boolean changeXtbmqx(String[] xtbms, String yhbh, int yhlx,
			String oldXtbm) {
		boolean flag = false;
		Session session = getSession();
		Connection con = null;
		try {
			con = session.connection();
			Statement st = con.createStatement();
			con.setAutoCommit(false);
			int num = 0;
			if (xtbms != null) {
				for (int i = 0; i < oldXtbm.split(",").length; i++) {
					num = 0;
					for (int j = 0; j < xtbms.length; j++) {
						if ((oldXtbm.split(",")[i] + "").equals(xtbms[j] + "")) {
							num = 1;
							break;
						}
					}
					if (num == 0) {
						sql = " delete from appsys_bmqx t where t.yhbh='"
								+ yhbh + "' and t.yhlb=" + yhlx
								+ " and t.xtbm = '" + oldXtbm.split(",")[i]
								+ "'";
						st.addBatch(sql);
						// 删除系统编码权限
						sql = " delete from appsys_xtbmqx t where t.yhbh='"
								+ yhbh + "' and t.yhlb=" + yhlx
								+ " and t.xtbm = '" + oldXtbm.split(",")[i]
								+ "'";
						st.addBatch(sql);
					}
				}
				for (int i = 0; i < xtbms.length; i++) {
					if (("," + oldXtbm + "").toUpperCase().indexOf(
							("," + xtbms[i].toUpperCase() + ",")) < 0) {
						// 保存系统编码权限
						sql = "insert into appsys_xtbmqx t (t.id,t.yhbh,t.xtbm,"
								+ "t.yhlb,t.scbz) values (seq_appsys_xtbmqx.nextval,"
								+ "'"
								+ yhbh
								+ "','"
								+ xtbms[i].toUpperCase()
								+ "'," + yhlx + ",0)";
						st.addBatch(sql);
					}
				}
			} else {
				// 删除部门权限
				sql = " delete from appsys_bmqx t where t.yhbh='" + yhbh
						+ "' and t.yhlb=" + yhlx;
				st.addBatch(sql);
				// 删除系统编码权限
				sql = " delete from appsys_xtbmqx t where t.yhbh='" + yhbh
						+ "' and t.yhlb=" + yhlx;
				st.addBatch(sql);
			}
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+changeXtbmqx", e1);
			}
			flag = false;
			log.error("Exception： XtbmqxDaoImpl+changeXtbmqx", e);
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				flag = false;
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+changeXtbmqx", e);
			}

		}

		return flag;
	}

	/**
	 * toDo:保存用户/用户组系统编码/部门权限
	 * 
	 * @param xtbm
	 * @param yhbh
	 * @param yhlx
	 * @param bmbms
	 * @return flag
	 */
	public boolean changeBmqx(String xtbm, String yhbh, int yhlx, String[] bmbms) {
		boolean flag = false;
		Session session = getSession();
		Connection con = null;
		try {
			con = session.connection();
			Statement st = con.createStatement();
			con.setAutoCommit(false);
			// 删除系统部门权限
			if (yhlx != -1) {
				sql = " delete from appsys_bmqx t where t.yhbh='" + yhbh
						+ "' and t.yhlb=" + yhlx + " and t.xtbm = '"
						+ xtbm.toUpperCase() + "' ";
				st.addBatch(sql);
			} else {
				yhlx = 1;
			}
			// 保存部门权限
			if (bmbms != null) {
				for (int i = 0; i < bmbms.length; i++) {
					sql = "insert into appsys_bmqx t(t.id,t.yhbh,t.yhlb,t.bmbm,"
							+ "t.xtbm,t.scbz) values (seq_appsys_bmqx.nextval,'"
							+ yhbh
							+ "',"
							+ ""
							+ yhlx
							+ ",'"
							+ bmbms[i]
							+ "','"
							+ xtbm.toUpperCase() + "',0)";
					st.addBatch(sql);
				}
			}
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+changeBmqx", e1);
			}
			flag = false;
			log.error("Exception： XtbmqxDaoImpl+changeBmqx", e);
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				flag = false;
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+changeBmqx", e);
			}

		}
		return flag;
	}

	/**
	 * toDo:根据部门编号删除部门权限
	 * @param bmbm
	 * @return boolean 
	 */
	public boolean deleteBmQxByBmbh(String[] bmbm) {
		boolean flag = false;
		Session session = getSession();
		Connection con = null;
		try {
			con = session.connection();
			Statement st = con.createStatement();
			con.setAutoCommit(false);
			// 删除系统部门权限
			for (int i = 0; i < bmbm.length; i++) {
				sql = " delete from appsys_bmqx t" +
						" where t.bmbm ='" + bmbm[i] +"'";
				st.addBatch(sql);
			}
			st.executeBatch();
			con.commit();
			con.setAutoCommit(true);
			flag = true;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			try {
				con.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+deleteBmQxByBmbh", e1);
			}
			flag = false;
			log.error("Exception： XtbmqxDaoImpl+deleteBmQxByBmbh", e);
		} finally {
			try {
				if (con != null) {
					con.close();
				}
			} catch (SQLException e) {
				flag = false;
				// TODO Auto-generated catch block
				log.error("Exception： XtbmqxDaoImpl+deleteBmQxByBmbh", e);
			}

		}
		return flag;
	}
}
